package com.my.controller;

import cn.hutool.core.util.StrUtil;
import com.my.pojo.Branch;
import com.my.pojo.Department;
import com.my.service.BranchService;
import com.my.service.DepartmentService;
import com.my.service.impl.BranchServiceImpl;
import com.my.service.impl.DepartmentServiceImpl;
import com.my.vo.DepartmentVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/branch/departmentServlet")
public class DepartmentServlet extends HttpServlet {

    DepartmentService departmentService = new DepartmentServiceImpl();
    BranchService branchService = new BranchServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String departIdStr = req.getParameter("departId");
        if(StrUtil.isEmpty(departIdStr)){
            List<DepartmentVo> departmentList = departmentService.getAllDepartment();
            req.setAttribute("departmentList",departmentList);
        }else {
            Integer departId = Integer.parseInt(departIdStr);
            DepartmentVo departmentVo = departmentService.getDepartmentById(departId);

            List<Branch> branchList = branchService.getBranchBy(Branch.builder().build());
            Map<String,Object> departAndBranchMap =new HashMap();
            departAndBranchMap.put("department",departmentVo);
            departAndBranchMap.put("branchList",branchList);
            req.setAttribute("departAndBranchMap", departAndBranchMap);
            req.getRequestDispatcher("editDepartment.jsp").forward(req,resp);
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("UTF-8");
        String departName = req.getParameter("departName");
        Department department = Department.builder().departName(departName).build();
        List<DepartmentVo> departmentVos = departmentService.getDepartmentBy(department);
        if(departmentVos ==null && departmentVos.size()<=0){
            String branchIdStr = req.getParameter("branchId");
            String tel = req.getParameter("tel");
            String phone = req.getParameter("phone");
            String fax = req.getParameter("fax");
            String remark = req.getParameter("remark");
            Department department1 = Department.builder().branchId(Integer.parseInt(branchIdStr)).tel(tel).phone(phone).fax(fax).remark(remark).build();
            departmentService.addgetDepartment(department1);
        }
        resp.sendRedirect("departmentList.jsp");
    }
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String departIdStr = req.getParameter("departId");
        if(StrUtil.isNotEmpty(departIdStr)){
            Integer id = Integer.parseInt(departIdStr);
            departmentService.deleteDepartmentById(id);
        }
        resp.sendRedirect("abblication/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.write("(\"success\";"+true+")");
    }
}
